package com.parenting.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parenting.entities.OrdersDO;
import com.parenting.model.IMPage;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.OrderSearchListCriteriaRequest;
import com.parenting.response.OrderDetailsResponse;
import com.parenting.response.OrderListResponse;
import com.parenting.service.OrdersService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表  前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-18
 */
@RestController
@Api(tags = "订单管理", description = "订单列表 与 详情查询")
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService  ordersService;
    @ApiOperation("订单列表查询")
    @PostMapping("search")
    public Result<IMPage<OrderListResponse>> getOrderList(@RequestBody OrderSearchListCriteriaRequest request){

       return Result.buildSuccess(ordersService.getOrderList(request, AuthUtil.getUserInfo().getShopId()));
    }

    @ApiOperation("查看订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true)
    @GetMapping("{orderId}")
    public Result<OrderDetailsResponse> getOrderDetailsById(@PathVariable("orderId") String orderId){
        OrdersDO o = ordersService.getById(orderId);
        OrderDetailsResponse orderDetailsResponse = BeanUtil.copyProperties(o, OrderDetailsResponse.class);

        if (null==o.getOriginalPrice()){
            o.setOriginalPrice(0);
        }
        if (null==o.getItemConcessional()){
            o.setItemConcessional(0);
        }
        if (null==o.getItemNumber()){
            o.setItemNumber(0);
        }
        Integer  discount=(o.getOriginalPrice()-o.getItemConcessional())*o.getItemNumber();
        orderDetailsResponse.setDiscount(discount);
        return Result.buildSuccess(orderDetailsResponse);
    }


    public  Result getOrderStatistics(){
        return null;
    }

}

